package tn.orange.canreminder;

import java.util.ArrayList;
import java.util.List;

import org.apache.http.NameValuePair;

import tn.orange.canreminder.adapter.PlaceCategoryAdapter;
import tn.orange.canreminder.mock.MockObject;
import tn.orange.canreminder.model.PlacesCategory;
import tn.orange.canreminder.service.PlacesCategoriesService;
import tn.orange.canreminder.service.PlacesCategoriesServiceImp;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;

public class ListPlacesCategory extends Activity {

	private ListView listView;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.places_category);
		listView = (ListView) findViewById(R.id.list_places_category);
		CategoryAsyncTask asyncTask = new CategoryAsyncTask();
		asyncTask.execute();
	}

	private class CategoryAsyncTask extends
			AsyncTask<Void, Void, List<PlacesCategory>> {
		private final ProgressDialog dialog = new ProgressDialog(
				ListPlacesCategory.this);

		// can use UI thread here
		protected void onPreExecute() {
			this.dialog.setMessage("Veuillez patienter...");
			this.dialog.setCancelable(false);
			this.dialog.show();
		}

		// automatically done on worker thread (separate from UI thread)
		protected List<PlacesCategory> doInBackground(final Void... args) {

			List<PlacesCategory> categories = null;
			PlacesCategoriesService service = new PlacesCategoriesServiceImp();
			try {
				List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
				categories = service.getAllCategories(nameValuePairs);
				// categories = MockObject.getMockPlacesCategory();
				if (categories == null)
					throw new NullPointerException();
			} catch (Exception e) {
				// TODO Auto-generated catch block
				cancel(false);
			}
			return categories;
		}

		@Override
		protected void onCancelled() {
			// TODO Auto-generated method stub
			super.onCancelled();
			if (this.dialog.isShowing()) {
				this.dialog.dismiss();
			}
			showErrorDialog("Problème de connexion ");

		}

		// can use UI thread here
		protected void onPostExecute(final List<PlacesCategory> categories) {
			if (this.dialog.isShowing()) {
				this.dialog.dismiss();

			}

			listView.setAdapter(new PlaceCategoryAdapter(
					ListPlacesCategory.this, categories));

			listView.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> arg0, View arg1,
						int arg2, long arg3) {
					// TODO Auto-generated method stub
					Intent intent = new Intent(ListPlacesCategory.this,
							ListPlaceActivity.class);

					intent.putExtra("category_id", categories.get(arg2).getId());

					startActivity(intent);
				}
			});

		}

		private void showErrorDialog(String message) {
			AlertDialog alertDialog = new AlertDialog.Builder(
					ListPlacesCategory.this).create();
			alertDialog.setTitle("Erreur");
			alertDialog.setMessage(message);
			alertDialog.setButton("Ok", new DialogInterface.OnClickListener() {
				public void onClick(DialogInterface dialog, int which) {
					ListPlacesCategory.this.finish();
				}
			});

			alertDialog.show();
		}

	}
	
	public void OnHomeClick(View v) {
		Intent intent = new Intent(getApplicationContext(),
				DashboardActivity.class);
		startActivity(intent);
	}
}
